﻿using System;
using System.Collections.Generic;
using System.Web.UI;
using System.Web.UI.WebControls;
using Dala.Samples.BusinessLogicLayer;
using Dala.Samples.DataAccessLayer;

namespace Dala.Samples.Grid_View
{
    public partial class grid_without_ods : System.Web.UI.Page
    {
        #region "Public Properties"

        private const int pageSize = 4;
        private const int pagerPageSize = 3;

        public Int32 _CURRENT_PAGE
        {
            get
            {
                if (ViewState["_CURRENT_PAGE"] != null)
                {
                    return Convert.ToInt32(ViewState["_CURRENT_PAGE"]);
                }
                else
                {
                    return 0;
                }
            }
            set
            {
                ViewState["_CURRENT_PAGE"] = value;
            }
        }

        private int TOTAL_COUNT
        {
            get
            {
                return new ProductBLL().GetTotalCountOfProductPaged(string.Empty, 0, pageSize);
            }
        }

        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ucBreadCrumb.BindBreadCrumb();
                BindData(1);
                Page.ClientScript.RegisterStartupScript(this.GetType(), "Script",
                                                        "SetActivePageIndex(" + (1) + ")", true);
            }
        }

        private void BindData(int pageIndex)
        {
            _CURRENT_PAGE = pageIndex;

            List<Product> _product = new ProductBLL().GetAllProductPaged(string.Empty, (pageIndex - 1) * pageSize, pageSize);

            gridViewProduct.DataSource = _product;
            gridViewProduct.DataBind();

            BindPager(pageIndex);
        }

        #region Pager Related Methods

        protected void repPager_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            int currentPage = 0;
            if (e.CommandArgument.Equals(">>"))
                BindNextPrevPage(true);
            else if (e.CommandArgument.Equals("<<"))
                BindNextPrevPage(false);
            else
            {
                currentPage = Convert.ToInt32(e.CommandArgument);
                BindData(currentPage);
            }
            Page.ClientScript.RegisterStartupScript(this.GetType(), "Script", "SetActivePageIndex(" + (_CURRENT_PAGE) + ")", true);
        }

        private void BindPager(int pageIndex)
        {
            int totalCount = TOTAL_COUNT;

            double pageInDouble = (double)totalCount / pageSize;
            Int32 pageCount = Convert.ToInt32(Math.Ceiling(pageInDouble)) + 1;

            List<string> pager = new List<string>();

            int pagerIndexValue = pageIndex == pagerPageSize ? 1 : pageIndex - (pageIndex % pagerPageSize == 0 ? pagerPageSize : pageIndex % pagerPageSize) + 1;

            int remainingPage = pageCount - pagerIndexValue;
            int pagerShow = pagerPageSize + pagerIndexValue;
            if (remainingPage < pagerPageSize)
            {
                pagerShow = pagerIndexValue + remainingPage;
            }
            if (pageIndex > pagerPageSize)
            {
                pager.Add("<<");
            }
            int i = 0;
            for (i = pagerIndexValue; i < pagerShow; i++)
            {
                pager.Add(i.ToString());
            }
            if (remainingPage >= pagerPageSize)
            {
                pager.Add(">>");
            }
            if (totalCount == 0)
            {
                pager.Clear();
                pager.Add("No Data Found");
            }

            repPager.DataSource = pager;
            repPager.DataBind();
        }

        private void BindNextPrevPage(bool isNext)
        {
            int currentPageIndex = _CURRENT_PAGE;
            int totalCount = TOTAL_COUNT;

            double pageInDouble = (double)totalCount / pageSize;
            Int32 pageCount = Convert.ToInt32(Math.Ceiling(pageInDouble)) + 1;

            if (isNext)
            {
                currentPageIndex = currentPageIndex == pagerPageSize ? pagerPageSize + 1 : currentPageIndex - (currentPageIndex % pagerPageSize == 0 ? pagerPageSize : currentPageIndex % pagerPageSize) + pagerPageSize + 1;
            }
            else
            {
                currentPageIndex = currentPageIndex == pagerPageSize ? 1 : currentPageIndex - (currentPageIndex % pagerPageSize == 0 ? pagerPageSize : currentPageIndex % pagerPageSize) - pagerPageSize + 1;
            }

            BindData(currentPageIndex);
        }

        #endregion
    }
}